//
//  ErWeiMaView.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/7/15.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "ErWeiMaView.h"
#import "UIView+circular.h"

#define viewH 260
#define viewW viewH
@interface ErWeiMaView()
@property (nonatomic,strong)UIView *blckView;
@end

@implementation ErWeiMaView

+(instancetype)createView{
    
    ErWeiMaView *view = [[[NSBundle mainBundle]loadNibNamed:@"ErWeiMaView" owner:nil options:nil]lastObject];
    [view viewToCircular];
    view.frame = CGRectMake(0, 0, viewW, viewH);
    view.hidden = YES;
   
    return view;
}

-(void)showInView:(UIView*)view{
    
    self.hidden = NO;

    if (!self.blckView) {
        self.blckView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
        [self.blckView setBackgroundColor:[UIColor blackColor]];
        UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [self.blckView addGestureRecognizer:singleTap];
    }
    
    CGPoint point = CGPointMake((UIScreenWidth-viewW)/2.0 + viewW/2.0, (UIScreenHeight-64-viewH)/2.0 + viewH/2.0);
    [self setCenter:point];
    
    [self.blckView setAlpha:0];
   
   
    [view addSubview:self.blckView];
     [view addSubview:self];

    
    
    [UIView animateWithDuration:0.2 animations:^{
        
        
        [self.blckView setAlpha:0.6];
        [self setAlpha:1];
        
//        [self setFrame:buf];
        
        
    } completion:^(BOOL finished) {
        
    }];
}



-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    
    [self hide];
}



-(void)hide{
    
    CGRect buf = self.frame;
    buf.origin.y += buf.size.height;
    [UIView animateWithDuration:0.2 animations:^{
        
        [self.blckView setAlpha:0];
        
        [self setFrame:buf];
        
    } completion:^(BOOL finished) {
        
        [self.blckView removeFromSuperview];
        [self removeFromSuperview];
    }];
}


//-(void)actionTap{
//    
//    [self hide];
//}
//-(void)showInView:(UIView *)view{
//    self.hidden = NO;
//    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionTap)];
//    [self addGestureRecognizer:tap];
//    [view addSubview:self];
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDelay:1.5];
//    self.frame = CGRectMake(0, 0, UIScreenWidth, UIScreenHeight);
//    [self setBackgroundColor:[UIColor blackColor]];
////    [self setAlpha:0.5];
//    
//    [UIView commitAnimations];
//}
//
//-(void)hide{
//    
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDelay:1.5];
//    
////    [self setAlpha:0.0];
//    self.frame = CGRectMake(0, UIScreenHeight, UIScreenWidth, UIScreenHeight);
//    
//    [UIView commitAnimations];
//}
@end
